/**
 * Definition for singly-linked list.
 * public class ListNode {
 * int val;
 * ListNode next;
 * ListNode(int x) { val = x; }
 * }
 */
public class TwoSum {

    static ListNode headNode, currentNode, temp = null;
    static int hiVal, v1, v2, sum = 0;

    public static void main(String[] args) {
        ListNode temp = null;
        ListNode l1 = new ListNode(2);
        temp = new ListNode(4);
        l1.next = temp;
        temp.next = new ListNode(3);

        ListNode l2 = new ListNode(5);
        temp = new ListNode(6);
        l2.next = temp;
        temp.next = new ListNode(4);

        ListNode node = addTwoNumbers(l1, l2);
        System.out.println(node.toString());
    }

    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        while (null != l1 || null != l2 || hiVal > 0) {
            v1 = (null == l1) ? 0 : l1.val;
            v2 = (null == l2) ? 0 : l2.val;
            sum = v1 + v2 + hiVal;
            hiVal = sum / 10;
            temp = new ListNode(sum % 10);
            if (null == currentNode) {
                currentNode = temp;
                headNode = currentNode;
            } else {
                currentNode.next = temp;
                currentNode = currentNode.next;
            }
            l1 = null == l1 ? null : l1.next;
            l2 = null == l2 ? null : l2.next;
        }

        return headNode;
    }

    public static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }
}